package mediator.main.mediator;

import mediator.main.colleague.IUser;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class ConcreteMediator implements IMediator {
    private final List<IUser> userList = new ArrayList<>();

    /**
     * 注册用户信息
     */
    @Override
    public void regiest(IUser user) {
        if (user != null && !userList.contains(user)) {
            userList.add(user);
        }
    }

    /**
     * 发送消息给所有人
     */
    @Override
    public void notifyAllMessage(IUser user) {
        Iterator<IUser> iterator = userList.iterator();
        IUser tmpUser = null;
        while (iterator.hasNext()) {
            tmpUser = iterator.next();
            if (tmpUser != null && !tmpUser.equals(user)) {
                //排除发送消息用户
                tmpUser.receiveMessage(user.getMessage());
            }
        }
    }
}
